package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.*;
import com.tanhua.model.vo.SoulQuestionsVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class QuestionnaireApiImpl implements QuestionnaireApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查看当天的灵魂结果
     * @param userId
     * @param data
     * @return
     */
    @Override
    public List<SoulResult> checkHistory(Long userId, String data) {
        // 构建条件
        Criteria criteria = Criteria.where("userId").is(userId).and("date").is(data);
        // 返回数据
        List<SoulResult> soulResults = mongoTemplate.find(Query.query(criteria), SoulResult.class);
        return soulResults;
    }

    /**
     * 查看灵魂推荐
     * @param userId
     * @return
     */
    @Override
    public List<SoulRecommend> getRecommendStar(Long userId) {
        //构建条件
        Criteria criteria = Criteria.where("userId").is(userId);
        return mongoTemplate.find(Query.query(criteria),SoulRecommend.class);
    }

    /**
     * 查询灵魂题目
     * @param i
     * @param recommendStar
     * @return
     */
    @Override
    public Questionnaire prepareQuestionnaire(Integer i, Integer recommendStar) {

        Criteria criteria = Criteria.where("qId").is(i).and("star").is(recommendStar);
        Questionnaire questionnaire = mongoTemplate.findOne(Query.query(criteria), Questionnaire.class);
        return questionnaire;
    }

    /**
     * @param id 问卷的编号
     * @return
     */
    @Override
    public List<SoulQuestionsVo> prepareQuestions(Integer id) {
        // 1.根据问卷的ID查询出问卷，的题目和选型
        Criteria criteria = Criteria.where("qId").is(id);
        List<Questionnaire> questionnaires = mongoTemplate.find(Query.query(criteria), Questionnaire.class);
        List<SoulQuestionsVo> voss = new ArrayList<>();
        for (Questionnaire questionnaire : questionnaires) {
            Criteria criteria1 = Criteria.where("options").is(questionnaire.getQId());
            List<Questions> questions = mongoTemplate.find(Query.query(criteria1), Questions.class);
            for (Questions question : questions) {
                Criteria criteria2 = Criteria.where("questionId").is(question.getQId());
                List<Option> options = mongoTemplate.find(Query.query(criteria2), Option.class);
                SoulQuestionsVo vo = new SoulQuestionsVo();
                vo.setOptions(options);
                vo.setId(question.getQId().toString());
                vo.setQuestion(question.getQuestion());
                vo.setNaireId(question.getNaireId());
                voss.add(vo);;
            }
        }
        return voss;
    }

    @Override
    public void update(Questionnaire questionnaire) {
       mongoTemplate.insert(questionnaire);
    }
}
